<template>
  <div class="search">
    <el-card style="margin: 20px">
      <el-row>
        <el-col :span="18">
          <div class="grid-content bg-purple-dark">
            <div v-for="item in renderList" :key="item.name" class="item">
              <input-field v-if="item.type === 'input'" :item="item" />
              <select-field v-else :item="item" />
            </div>
            <slot />
          </div>
        </el-col>
        <el-col :span="6">
          <div class="grid-content btns">
            <el-button
              type="success"
              class="search"
              round
              @click="$emit('search')"
            >
              搜索
            </el-button>
            <el-button type="info" class="reset" round @click="resetvalue">
              重置
            </el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
// 导入下拉框配置选项
import selectFieldType from './selete.js'
// 导入输入框子组件 下拉框子组件
import inputField from './inputField.vue'
import selectField from './selectField.vue'

export default {
  components: { inputField, selectField },
  props: {
    searhBar: { type: Array, required: true }
  },
  computed: {
    renderList() {
      // 读取父组件传递过来的配置searhBar 如果有下拉框 加上下拉框的配置选项
      // 下拉框的配置选项在 ./selete.js 文件中
      return this.searhBar.map(item => {
        if (item.type === 'selete') {
          item.options = (selectFieldType[item.seleteType]).options
        }
        return item
      })
    }
  },
  // 接收重置函数
  inject: ['resetvalue']
}
</script>
<style scoped lang="scss">
.inputField {
  width: 210px;
}
.selectField {
  width: 210px;
  ::v-deep .el-select {
    width: 100%;
  }
}
.bg-purple-dark {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  .item {
    padding: 0 10px;
  }
}

.btns {
  padding-top: 40px;
  display: flex;
  justify-content: space-around;
  .el-button {
    width: 100px;
    color: black;
  }
  .search {
    background-color: $btn-search;
  }
  .reset {
    background-color: $btn-reset;
  }
}
</style>
